﻿<%@ WebHandler Language="C#" Class="xinxi_xinghao" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_xinghao : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["xingHaoType"];
            string wuliaobianhao = context.Request.Form["wuLiaoBianHao"];
            if (string.IsNullOrEmpty(requestType) || string.IsNullOrEmpty(wuliaobianhao))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        string createCondition = m_DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, wuliaobianhao, "", SearchConditionType.等于);
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_xinghao", createCondition);
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string xinghao = context.Request.Form["xinghao"];
                        string pianchazhongliang = context.Request.Form["pianchazhongliang"];
                        string pianchazhuangtai = context.Request.Form["pianchazhuangtai"];
                        string huansuangongshi = context.Request.Form["huansuangongshi"];
                        string morenkouzhong = context.Request.Form["morenkouzhong"];
                        string kouzhongfangshi = context.Request.Form["kouzhongfangshi"];
                        string xingHaoID = context.Request.Form["xingHaoID"];
                        string shuiwenliao = context.Request.Form["shuiwenliao"];
                        string hunningtu = context.Request.Form["hunningtu"];
                        string liqingliao = context.Request.Form["liqingliao"];
                        
                        if (string.IsNullOrEmpty(xingHaoID))
                        {
                            //新增
                            string xingHaoCondition = m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xinghao, "", SearchConditionType.等于);
                            xingHaoCondition += m_DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, wuliaobianhao, "", SearchConditionType.等于);
                            DataTable xingHaoDt = m_DB.GetDataTableByCondition("tbl_xinghao", xingHaoCondition);
                            if (xingHaoDt.Rows.Count > 0)
                            {
                                context.Response.Write("已经存在该型号，请检查重试...");
                                return;
                            }

                            TableRow newRow = new TableRow("tbl_xinghao", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol();
                            col1.ColName = "xinghao";
                            col1.ValueMember = xinghao;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "pianchazhongliang";
                            col2.ValueMember = pianchazhongliang;
                            col2.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "pianchazhuangtai";
                            col3.ValueMember = pianchazhuangtai;
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "huansuangongshi";
                            col4.ValueMember = huansuangongshi;
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "morenkouzhong";
                            col5.ValueMember = morenkouzhong;
                            col5.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "kouzhongfangshi";
                            col6.ValueMember = kouzhongfangshi;
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "wuliaobianhao";
                            col7.ValueMember = wuliaobianhao;
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol();
                            col8.ColName = "zhandian";
                            col8.ValueMember = "云端";
                            col8.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col8);
                            TableCol col9 = new TableCol();
                            col9.ColName = "kehubianhao";
                            col9.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col9.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col9);
                            TableCol col10 = new TableCol() { ColName = "shuiwenliao", ValueMember = shuiwenliao, ValueType = DataValueCommonType.字符串 };
                            TableCol col11 = new TableCol() { ColName = "hunningtu", ValueMember = hunningtu, ValueType = DataValueCommonType.字符串 };
                            TableCol col12 = new TableCol() { ColName = "liqingliao", ValueMember = liqingliao, ValueType = DataValueCommonType.字符串 };
                            newRow.DataCols.Add(col10);
                            newRow.DataCols.Add(col11);
                            newRow.DataCols.Add(col12);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            //修改
                            string xingHaoCondition = m_DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, xinghao, "", SearchConditionType.等于);
                            xingHaoCondition += m_DB.CreateAndCondition("id", DataValueCommonType.字符串, xingHaoID, "", SearchConditionType.不等于);
                            xingHaoCondition += m_DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, wuliaobianhao, "", SearchConditionType.等于);
                            DataTable xingHaoDt = m_DB.GetDataTableByCondition("tbl_xinghao", xingHaoCondition);
                            if (xingHaoDt.Rows.Count > 0)
                            {
                                context.Response.Write("已经存在该型号，请检查重试...");
                                return;
                            }
                            TableRow newRow = new TableRow("tbl_xinghao", xingHaoID, DBCtl.修改);
                            TableCol col1 = new TableCol();
                            col1.ColName = "xinghao";
                            col1.ValueMember = xinghao;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "pianchazhongliang";
                            col2.ValueMember = pianchazhongliang;
                            col2.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "pianchazhuangtai";
                            col3.ValueMember = pianchazhuangtai;
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "huansuangongshi";
                            col4.ValueMember = huansuangongshi;
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "morenkouzhong";
                            col5.ValueMember = morenkouzhong;
                            col5.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "kouzhongfangshi";
                            col6.ValueMember = kouzhongfangshi;
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "wuliaobianhao";
                            col7.ValueMember = wuliaobianhao;
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol();
                            col8.ColName = "zhandian";
                            col8.ValueMember = "云端";
                            col8.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col8);
                            TableCol col9 = new TableCol() { ColName = "shuiwenliao", ValueMember = shuiwenliao, ValueType = DataValueCommonType.字符串 };
                            TableCol col10 = new TableCol() { ColName = "hunningtu", ValueMember = hunningtu, ValueType = DataValueCommonType.字符串 };
                            TableCol col11 = new TableCol() { ColName = "liqingliao", ValueMember = liqingliao, ValueType = DataValueCommonType.字符串 };
                            newRow.DataCols.Add(col9);
                            newRow.DataCols.Add(col10);
                            newRow.DataCols.Add(col11);

                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }

                        break;
                    case "3":
                        //删除
                        string delXingHaoID = context.Request.Form["xingHaoID"];
                        if (string.IsNullOrEmpty(delXingHaoID))
                        {
                            context.Response.Write("没有找到该型号信息，请检查重试...");
                            return;
                        }
                        TableRow delRow = new TableRow();
                        delRow.TableName = "tbl_xinghao";
                        delRow.ID = delXingHaoID;
                        if (m_DB.DeleteRowByID(delRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("Failed");
                        }
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("型号信息异常。{0}", err.Message));
            context.Response.Write("请重试...");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}